<template>
    <!-- Main content -->
    <div>
        <div class="row">
            <div class="col-xs-12">
                <theme-box title="好券速递列表">
                    <form class="form-inline" @submit.prevent v-el:form>

                    </form>
                    <table class="table table-bordered table-striped" style="width:100%" v-el:table>
                    </table>
                </theme-box>
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </div>
    <!-- /.content -->
</template>

<script>
    import {
        createDataTable
    } from 'ui/collections/general.js'
    var serialize = require('form-serialize');
    import {dateTransform} from "lib/method.js"

    var dataTable;
    var columnsSet;
    export default {
        data: () => ({
            item: {},
            columnsSet: []
        }),
        methods: {
            //还原弹出框位置
            resetBindDrag() {
                this.$refs.modal._resetBindDrag();
            },
            //查询按钮
            search: function() {
                dataTable.ajax.reload();
            },
            //重置按钮
            reset: function() {
                $(".form-inline")[0].reset();
                dataTable.ajax.reload();
            }
        },
        created() {
            //datatable所用数组
            columnsSet = [{
                title: "商户号",
                data: "mchtId",
                visible:false,
                className: "operate"
            }, {
                title: "商户名称",
                data: "mchtName",
                edit: true,
                className: "operate"
            }, {
                title: "电子券号",
                data: "couponId",
                visible:false,
                className: "operate"
            },{
                title: "电子券名称",
                data: "name",
                className: "operate"
            },{
                title: "顺序",
                data: "seq",
                className: "operate",
            },{
                title: "剩余张数",
                data: "surplusnum",
                className: "operate",
            },{
                title: "有效期结束日期",
                data: "validatyEnd",
                className: "operate",
                render: function (data) {
                    return dateTransform(data)
                }
            },{
               title: "相关操作",
               className: "operate",
               width: "160px",
               render: function(value, type, row) {
                  return `
                    <button type="button" class="btn btn-danger btn-sm delete">删除</button>
                  `
                 }
               }]
        },
        ready: async function() {
            this.$set("columnsSet", columnsSet)
            var self = this;
            //创建dataTable
            dataTable = $(this.$els.table).DataTable({
                "columns": this.columnsSet, //定义列
                "ajax": async function(data, callback, settings) {
                    var page = [{
                        "name": "iDisplayStart",
                        "value": 1 + data.start / data.length
                    }, {
                        "name": "iDisplayLength",
                        "value": data.length
                    }];
                    //表单序列化数据
                    var _data = serialize(self.$els.form, {
                        hash: true,
                        disabled: true,
                        empty: true
                    });
                    _data.aoData = JSON.stringify(page);
                    //发送请求
                    var result = await self.$post({
                        url: "/api/v2" + projectName_Url + "/plat/CouponExpress/findCouponExpress",
                        data:_data,
                        method: "POST"
                    })
                    if (result.respCode != "00") {
                        Midway.alert({
                            title: "操作失败",
                            content: result.content
                        })
                    }
                    callback({
                        recordsTotal: result.content.total,
                        recordsFiltered: result.content.total,
                        data: result.content.list || []
                    })
                    //即将过期的优惠券 有效日期显示红色
                    if (result.content.list != '' && result.content.list != null) {
                        for (var i in result.content.list) {
                            //获取当前时间戳
                            var d= new Date();
                            var d2 = d.getFullYear() + "-"+ (d.getMonth() + 1) +"-"+ d.getDate();
                            //月是一位数时自动补齐0
                            if((d.getMonth() + 1) < 10){
                                var d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 1) +"-"+ d.getDate();
                            }
                            //日是一位数时自动补齐0
                            if(d.getDate() < 10){
                                var d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 1) +"-"+"0"+d.getDate();
                            }
                            var str = d2.replace(/-/g,'');

                            if(parseInt(result.content.list[i].validatyEnd)-parseInt(str) < 0){
                                var trs = $('tbody tr td:nth-child(5)');
                                trs.splice(0,4)
                                $(trs[i]).css('color','red');

                            }
                        }
                    }
                }
            });

            $(self.$el).on("click", ".delete", async function() {
                var Row = $(this).closest('tr').get(0);
                var name = dataTable.row(Row).data().name;
                var mchtId = dataTable.row(Row).data().mchtId;
                var couponId = dataTable.row(Row).data().couponId;

                await Midway.confirm({
                    title: "删除好券速递",
                    content: "是否要删除好券速递:" + name + "?"
                })
                var result = await self.$post({
                    url: "/api/v2" + projectName_Url + "/plat/CouponExpress/"+ mchtId +'/'+ couponId,
                    method: "DELETE"
                });
                Midway.alert({title: "操作提示", content: result.errorInfo})
                dataTable.ajax.reload();
            });
        },
        beforeDestroy() {
            $(this.$el).off("click")
        }
    }
</script>
